package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fy.fyzf.MainActivity;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.base.Contans;
import com.fy.fyzf.bean.LoginBean;
import com.fy.fyzf.presenter.LoginPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.DeviceInfoModel;
import com.fy.fyzf.utils.NetWorkUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.utils.Sha1Utils;
import com.fy.fyzf.view.UserLoginView;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

import static com.fy.fyzf.api.ApiConstant.ALIAS;

public class LoginActivity extends BaseActivity<LoginPresenter> implements UserLoginView{


    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @BindView(R.id.tv_join_us)
    TextView tvJoinUs;

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter( this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        etAccount.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        addDisposable(RxView.clicks(tvLogin).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {
            Contans.REQUEST_TYPE = 0;
            String pwd = etPwd.getText().toString();
            String sha1 = Sha1Utils.getSha1(pwd);
            if (TextUtils.isEmpty(etAccount.getText().toString()) || TextUtils.isEmpty(etPwd.getText().toString())) {
                AppUtils.showToast("用户名或密码不能为空");
                return;
            }

            String ip = NetWorkUtils.getLocalIpAddress();
            mPresenter.userLogin(DeviceInfoModel.getInstance().getPhoneModel()+""+ip,etAccount.getText().toString(),sha1);

        }));
    }

    @Override
    public void initData() {
        super.initData();
    }

    @OnClick({R.id.tv_login, R.id.tv_forget_pwd, R.id.tv_join_us})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
              break;
            case R.id.tv_forget_pwd:
                break;
            case R.id.tv_join_us:
                startActivity(new Intent(this,RegisterActivity.class));
                break;
        }
    }

    @Override
    public void userLoginSuccess(LoginBean loginBean) {
        //登录之后先保存token
        SPUtils.getInstance(LoginActivity.this).putString("author",loginBean.getToken());
        SPUtils.getInstance(LoginActivity.this).putString("name",loginBean.getName());
        SPUtils.getInstance(LoginActivity.this).putString("company",loginBean.getCompanyName());
        SPUtils.getInstance(LoginActivity.this).putString("userId",loginBean.getAlias());
        SPUtils.getInstance(LoginActivity.this).putString("userId",loginBean.getAlias());
        startActivity(new Intent(this, MainActivity.class));
        //设置极光推送唯一设备标识
        JPushInterface.setAlias(this,0,ALIAS+loginBean.getAlias());
        finish();
    }

    @Override
    public void onError() {

    }
}